package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.GoodsWetPrice;
import com.dingreading.cloud.admin.entity.table.GoodsWetPriceTableDef;
import com.dingreading.cloud.admin.mapper.GoodsWetPriceMapper;
import com.dingreading.cloud.admin.service.GoodsWetPriceService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2023-11-22
 */
@Service
public class GoodsWetPriceServiceImpl extends BaseServiceImpl<GoodsWetPriceMapper, GoodsWetPrice, GoodsWetPriceTableDef> implements GoodsWetPriceService {

    @Override
    protected GoodsWetPriceTableDef getTable() {
        return GoodsWetPriceTableDef.goodsWetPrice;
    }

    @Override
    public GoodsWetPrice getByGoodsIdAndGoodsTypeId(Long goodsId, Long goodsTypeId) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.goodsId.eq(goodsId))
                .and(table.goodsTypeId.eq(goodsTypeId));
        return getMapper().selectOneByCondition(condition);
    }

    @Override
    public List<GoodsWetPrice> getByGoodsTypeId(List<Long> goodsTypeIds) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.goodsTypeId.in(goodsTypeIds));
        return getMapper().selectListByCondition(condition);
    }

    @Override
    public boolean updateWetAvgPrice(Long id, Long goodsTypeId, int usableNum, double usablePrice, double wetAvgPrice) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id))
                .and(table.goodsTypeId.eq(goodsTypeId));
        if (wetAvgPrice != 0) {
            return UpdateChain.of(GoodsWetPrice.class)
                    .set(table.usableNum, usableNum)
                    .set(table.usablePrice, usablePrice)
                    .set(table.wetAvePrice, wetAvgPrice)
                    .where(condition)
                    .update();
        } else {
            return UpdateChain.of(GoodsWetPrice.class)
                    .set(table.usableNum, usableNum)
                    .set(table.usablePrice, usablePrice)
                    .where(condition)
                    .update();
        }
    }

    @Override
    public boolean updateUsableNumAndUsablePrice(Long id, Long goodsTypeId, int usableNum, double usablePrice) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.id.eq(id))
                .and(table.goodsTypeId.eq(goodsTypeId));
        return UpdateChain.of(GoodsWetPrice.class)
                .set(table.usableNum, usableNum)
                .set(table.usablePrice, usablePrice)
                .where(condition)
                .update();
    }

    @Override
    public Map<Long, GoodsWetPrice> mapByGoodsTypeIdIn(List<Long> goodsTypeIds) {
        List<GoodsWetPrice> list = getMapper().selectListByCondition(table.goodsTypeId.in(goodsTypeIds));

        Map<Long, GoodsWetPrice> map = null;
        if (CollectionUtils.isNotEmpty(list)) {
            map = list.stream().collect(Collectors.toMap(GoodsWetPrice::getGoodsTypeId, Function.identity()));
        }
        return map;
    }

}
